package com.git.smp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.git.smp.core.PageRequest;
import com.git.smp.core.ResponseMessage;
import com.git.smp.dao.MenuMapper;
import com.git.smp.entity.Menu;
import com.git.smp.entity.MenuExtend;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/menu")
public class MenuController {

	Logger logger = LoggerFactory.getLogger(MenuController.class);

	@Autowired
	MenuMapper menuMapper;
	
	@Autowired
	private HttpServletRequest request;
	
	/**
	 * 获取用户菜单
	 * @return
	 */
	@RequestMapping("/getMenusByUserId")
	public ResponseMessage<List<Menu>> getMenusByUserId() {
		ResponseMessage<List<Menu>> responseMessage = new ResponseMessage<List<Menu>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("查询成功！");
		
		String applyUser = request.getHeader("userName");
		List<Menu> listMenu = menuMapper.getMenusByUserId(applyUser);
		
		if (listMenu == null) {
			responseMessage.setCode("0001");
			
			responseMessage.setMessage("查询菜单信息失败！");
		}
		responseMessage.setExtend(listMenu);
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}
	
	
	

	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/selectByPage")
	public ResponseMessage<PageInfo<Menu>> selectByPage(@RequestBody PageRequest<Menu> pageRequest) {
		ResponseMessage<PageInfo<Menu>> responseMessage = new ResponseMessage<PageInfo<Menu>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("查询成功！");

		Menu menu = pageRequest.getBody();

		PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
		List<Menu> listMenu = menuMapper.select(menu);
		if (listMenu == null) {
			responseMessage.setCode("0001");
			
			responseMessage.setMessage("查询菜单信息失败！");
		}
		PageInfo<Menu> pageInfo = new PageInfo<Menu>(listMenu, pageRequest.getPageSize());
		responseMessage.setExtend(pageInfo);
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}
	
	
	

	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/addMenu")
	public ResponseMessage addMenu(@RequestBody Menu menu) {
		ResponseMessage<PageInfo<Menu>> responseMessage = new ResponseMessage<PageInfo<Menu>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("用户新增成功！");
		try {
			menuMapper.insertSelective(menu);
		} catch (Exception e) {
			logger.error("",e);
			responseMessage.setCode("0001");
			responseMessage.setMessage("用户新增失败！");
		}

		return responseMessage;
	}
	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/updateMenu")
	public ResponseMessage updateMenu(@RequestBody Menu menu) {
		ResponseMessage<PageInfo<Menu>> responseMessage = new ResponseMessage<PageInfo<Menu>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("用户修改成功！");
		try {
			menuMapper.updateByPrimaryKeySelective(menu);
		} catch (Exception e) {
			logger.error("",e);
			responseMessage.setCode("0001");
			responseMessage.setMessage("用户修改失败！");
		}

		return responseMessage;
	}
	
	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/deleteMenu")
	public ResponseMessage deleteMenu(@RequestBody Menu menu) {
		ResponseMessage<PageInfo<Menu>> responseMessage = new ResponseMessage<PageInfo<Menu>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("用户删除成功！");
		
		try {
			menuMapper.delete(menu);
		} catch (Exception e) {
			logger.error("",e);
			responseMessage.setCode("0001");
			responseMessage.setMessage("用户删除失败！");
		}
		
		return responseMessage;
	}
	
	//获取所有菜单信息
	@RequestMapping("/getMenuList")
	public ResponseMessage getMenuList() {
		
		ResponseMessage<List<MenuExtend>> responseMessage = new ResponseMessage<List<MenuExtend>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("查询所有菜单信息成功！");
		
		List<MenuExtend> listMenu = menuMapper.getMenuList();
		
		if (listMenu == null) {
			responseMessage.setCode("0001");
			responseMessage.setMessage("查询所有菜单信息失败！");
		}
		List<MenuExtend> listMenuExtend = new ArrayList<MenuExtend>();
		for (MenuExtend listMenu1 : listMenu) {
            if ("1".equals(listMenu1.getlevel())) { //一级菜单
                List<MenuExtend> m1 = new ArrayList<MenuExtend>();
                for (MenuExtend listMenu2 : listMenu) {
                    if (listMenu1.getName().equals(listMenu2.getparent())) {
                        m1.add(listMenu2);
                    }
                }
                listMenu1.setNode(m1);
                listMenuExtend.add(listMenu1);
            }
        }
		responseMessage.setExtend(listMenuExtend);
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}
	
	
	/**
	 * 获取父级菜单
	 * @return
	 */
	@RequestMapping("/getParent")
	public ResponseMessage getParent() {
		
		ResponseMessage<List<Menu>> responseMessage = new ResponseMessage<List<Menu>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("查询父菜单信息成功！");
		
		List<Menu> listMenu = menuMapper.getParent();
		
		if (listMenu == null) {
			responseMessage.setCode("0001");
			responseMessage.setMessage("查询父菜单信息失败！");
		}
		
		responseMessage.setExtend(listMenu);
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}
	
}
